package com.cfets.annualAssessment.service;

import com.cfets.annualAssessment.entity.*;
import com.cfets.annualAssessment.mapper.*;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import tk.mybatis.mapper.entity.Example;
import java.util.List;

/**
 * Created by ShenYiChou on 2017/1/19.
 */
@Service
public class QuestionService {
    @Autowired
    private QuesAllMapper quesAllMapper;

    @Autowired
    private QuesConfMapper quesConfMapper;

    @Autowired
    private QuesStatMapper quesStatMapper;

    @Autowired
    private AnnualInformMapper annualInformMapper;

    @Autowired
    private QuesTimeMapper quesTimeMapper;

    public List<Question> getAllQuestionPage(Question question) {
        Page<?> page = PageHelper.startPage(question.getPage(), question.getRows());
        List<Question> questionList = quesAllMapper.selectAll();
        question.setTotalCount(page.getTotal());
        return questionList;
    }
    public List<Question> getAllQuestion() {
        return this.quesAllMapper.selectAll();
    }
    public boolean insertQuestion(Question question) {
        return this.quesAllMapper.insertSelective(question)>0;
    }
    public int QuestionsOrNot(int question_num){
        int total = quesAllMapper.countAllQuestions();
        if(question_num>total){
            return -2;
        }
        return 1;
    }

    public List<Question> newQuestions(){
        List<QuestionConf> questionConfList = quesConfMapper.selectAll();
        int question_num = questionConfList.get(0).getQuestion_num();
        return quesAllMapper.selectRandomQuestions(question_num);
    }
    public QuestionConf getQuestionConf (){
        Example example=new Example(QuestionConf.class);
        example.setOrderByClause("post_time DESC");
        List<QuestionConf> questionConfList = quesConfMapper.selectByExample(example);
        return questionConfList.get(0);
    }

    public List<QuestionState> getQuestionState (String ca_num){
        Example example=new Example(QuestionState.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(ca_num)){
            criteria.andEqualTo("ca_num",ca_num);
        }
        List<QuestionState> questionStates = quesStatMapper.selectByExample(example);
        return questionStates;
    }

    public boolean newQuestionConf(QuestionConf questionConf) {
        return quesConfMapper.insertSelective(questionConf)>0;
    }

    public boolean newQuestionState(QuestionState questionState) {
        return quesStatMapper.insertSelective(questionState)>0;
    }

    public boolean newQuesTimeState(QuesTimeState quesTimeState) {
        return quesTimeMapper.insertSelective(quesTimeState)>0;
    }

    public boolean updateQuestionConf(QuestionConf questionConf) {
        return quesConfMapper.updateByPrimaryKey(questionConf)>0;
    }
    public boolean updateQuestionState(QuestionState questionState) {
        Example example=new Example(QuestionState.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(questionState.getCa_num())){
            criteria.andEqualTo("ca_num",questionState.getCa_num());
        }
        if(!StringUtils.isEmpty(questionState.getQues_id())){
            criteria.andEqualTo("ques_id",questionState.getQues_id());
        }
        return quesStatMapper.updateByExample(questionState,example)>0;
    }
    public boolean updateQuestionTime(QuesTimeState quesTimeState) {
        Example example=new Example(QuesTimeState.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(quesTimeState.getCa_num())){
            criteria.andEqualTo("ca_num",quesTimeState.getCa_num());
        }
        return quesTimeMapper.updateByExample(quesTimeState,example)>0;
    }
//    public List<QuestionNow> getQuestionNow (){
//        List<QuestionNow> questionNowList = quesNowMapper.selectAll();
//        return questionNowList;
//    }

    public List<Question> getQuestionNowState (String ca_num){
        return quesAllMapper.selectQuesNowState(ca_num);
    }

    public boolean delQuestionNowState (String ca_num){
        Example example=new Example(QuestionState.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(ca_num)){
            criteria.andEqualTo("ca_num",ca_num);
        }
        return quesStatMapper.deleteByExample(example)>0;
    }

    public boolean delQuestionTimeState (String ca_num){
        Example example=new Example(QuesTimeState.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(ca_num)){
            criteria.andEqualTo("ca_num",ca_num);
        }
        return quesTimeMapper.deleteByExample(example)>0;
    }

    public boolean delAllQuestion (){
        Example example=new Example(Question.class);
        return quesAllMapper.deleteByExample(example)>0;
    }

    public CommPara getScore(String ca_num){
        CommPara commPara = new CommPara();
        int score=0;
        List<Question> questionList = this.getQuestionNowState(ca_num);
        for(Question item:questionList){
            if(item.getAnswer().equals(item.getOption_state())){
                score++;
            }
        }
        commPara.setCommInt(score);
        QuestionConf questionConf = this.getQuestionConf();
        if(score>=questionConf.getQuestion_pass()){
            commPara.setCommString("通过");
        }else{
            commPara.setCommString("不通过");
        }
        //考试chance减1
        Example example=new Example(AnnualInform.class);
        Example.Criteria criteria =example.createCriteria();
        if(!StringUtils.isEmpty(ca_num)){
            criteria.andEqualTo("ca_num",ca_num);
        }
        List<AnnualInform> annualInformList = this.annualInformMapper.selectByExample(example);
        for(AnnualInform item:annualInformList){
            if(item.getChance()!=0) {
                item.setChance((item.getChance() - 1));
                this.annualInformMapper.updateByPrimaryKey(item);
            }
        }


        return commPara;
    }
    //考试时间和考试chance   时间：考试时间初始化
    public CommPara getQuesTime(String ca_num){
        CommPara commPara = new CommPara();
        QuestionConf questionConf = this.getQuestionConf();
        int time_init = questionConf.getQuestion_time();
        commPara.setCommInt(this.AbleTestOrNot(ca_num));//考试机会
        Example example = new Example(QuesTimeState.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(ca_num)) {
            criteria.andEqualTo("ca_num", ca_num);
        }
        List<QuesTimeState> quesTimeStates = quesTimeMapper.selectByExample(example);
        if(quesTimeStates.isEmpty()){
            QuesTimeState quesTimeState = new QuesTimeState();
            quesTimeState.setCa_num(ca_num);
            quesTimeState.setTimer_init(time_init);
            quesTimeState.setIndex_now(0);
            this.newQuesTimeState(quesTimeState);
            commPara.setCommInt2(time_init);
            return commPara;
        }
        commPara.setCommInt2(quesTimeStates.get(0).getTimer_init());
        return commPara;
    }
    //-1：考试机会没了
    public int AbleTestOrNot(String ca_num) {
        Example example = new Example(AnnualInform.class);
        Example.Criteria criteria = example.createCriteria();
        if (!StringUtils.isEmpty(ca_num)) {
            criteria.andEqualTo("ca_num", ca_num);
        }
        List<AnnualInform> annualInformList = this.annualInformMapper.selectByExample(example);
        for(AnnualInform item : annualInformList){
            if (item.getChance() == 0) {
                break;
            } else {
                return item.getChance();
            }
        }
        return -1;
    }
}
